import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_getx/view/tab/logic.dart';

/// 页面自动刷新Mixin
/// 使用方式：在Logic类中混入此Mixin，并调用setupPageRefresh
mixin PageRefreshMixin on GetxController {
  /// 设置页面自动刷新
  /// [tabIndex] 当前页面在Tab中的索引
  /// [refreshCallback] 刷新回调函数
  void setupPageRefresh(int tabIndex, VoidCallback refreshCallback) {
    // 获取TabState
    final tabState = Get.find<TabLogic>().state;

    // 监听页面切换
    ever(tabState.currentIndex, (index) {
      if (index == tabIndex) {
        print('PageRefreshMixin: 检测到切换到页面索引$tabIndex，执行刷新');
        refreshCallback();
      }
    });
  }
}
